#include "FigureContainer.h"


FigureContainer* FigureContainer::instance = NULL;
std::map<std::string,Figure*> FigureContainer::figures;

FigureContainer::FigureContainer(){}


void FigureContainer::destroy(){
	std::map<std::string, Figure*>::iterator it;

	for(it=figures.begin();it!=figures.end();it++){
		Figure* figure = (*it).second;
		delete figure;
	}

	if(FigureContainer::instance!=NULL)
		delete FigureContainer::instance;
	FigureContainer::instance = NULL;
}

FigureContainer::~FigureContainer(){
}

FigureContainer* FigureContainer::getInstance(){
	if(!FigureContainer::instance)
		FigureContainer::instance = new FigureContainer();
	return FigureContainer::instance;
}

/****************************************************************
 * Devuelve un objeto Figure con el id figureId si no encuentra *
 * el id deberia lanzar un ObjectNotFoundExeption               *
 ***************************************************************/

Figure* FigureContainer::getFigureById(std::string figureId){
	std::map<std::string, Figure*>::iterator it;
	it = this->figures.find(figureId);
	if(it!=this->figures.end())
		return it->second;
	return NULL;
}

void FigureContainer::addFigure(Figure* figure)throw (IdAlreadyExistsException){
	if(figures[figure->getId()]!=NULL)
		throw IdAlreadyExistsException("Ya existe una figura con el id: "+figure->getId());
	else 
		figures[figure->getId()]=figure;
}

const std::map<std::string,Figure*> FigureContainer::getAllFigures() const{
	return this->figures;
}

